package jui.maps
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import jui.business.ManagersFactory;
	import jui.business.QuickReplyManager;
	import jui.business.XMPPManager;
	
	import modules.webui.events.WebUIEvent;
	
	import mx.binding.utils.BindingUtils;
	
	public class WebUIEventMap extends EventDispatcher implements IJUIEventMap 
	{
		private var app: JUI;
		private var xmppManager: XMPPManager;
		private var replyManager: QuickReplyManager;
		
		public function setManagers(): void
		{
			var gm: Function = ManagersFactory.getInstance().getManager;
			xmppManager = gm( XMPPManager.getQName() );
			replyManager = gm( QuickReplyManager.getQName() );
			
			BindingUtils.bindSetter(app.replyPanel.messageInput.setInputMessageText, replyManager, "inputText");
			BindingUtils.bindProperty(app.replyPanel, "x", replyManager, "inputPanelX");
			BindingUtils.bindProperty(app.replyPanel, "y", replyManager, "inputPanelY");
			BindingUtils.bindProperty(app.replyPanel, "visible", replyManager, "inputPanelVisible");
		}
		
		public function setHandlers(): void
		{
			app.systemManager.addEventListener(WebUIEvent.REPLY_BY_ID, webUIReplyByIdHandler);
			app.systemManager.addEventListener(WebUIEvent.CANCEL_REPLY, webUICancelReplyHandler);
		}
		
		//----------------------------------------------------------------------
		// Event Handlers go gere
		//----------------------------------------------------------------------
		
		private function webUIReplyByIdHandler(event:WebUIEvent): void
		{
			replyManager.quickReplyById(event.replyId, event.eventX, event.eventY);
		}
		
		private function webUICancelReplyHandler(event:WebUIEvent): void
		{
			replyManager.cancelReply();
		}
		
		public function WebUIEventMap(target:IEventDispatcher=null, app: JUI=null): void
		{
			if(instance) throw new Error("WebUIEventMap is singleton");
			this.app = app;
			setManagers();
			setHandlers();
		}
		
		public static var instance: WebUIEventMap;
	}
}